<?php

class saveDeleteResearchProjectAction extends basePimAction
{

    /**
     * @param sfForm $form
     * @return
     */
    public function setResearchProjectForm(sfForm $form)
    {
        if (is_null($this->researchProjectForm)) {
            $this->researchProjectForm = $form;
        }
    }

    public function execute($request)
    {

        $employeeResearchProject = $request->getParameter('employeeResearchProject');
        $empNumber = (isset($employeeResearchProject['emp_number'])) ? $employeeResearchProject['emp_number'] : $request->getParameter('empNumber');

        if (!$this->IsActionAccessible($empNumber)) {
            $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
        }

        $this->employeeResearchProjectPermissions = $this->getDataGroupPermissions('qualification_research_project', $empNumber);

        $this->setResearchProjectForm(new EmployeeResearchProjectForm(array(), array('empNumber' => $empNumber, 'employeeResearchProjectPermissions' => $this->employeeResearchProjectPermissions), true));

        //this is to save research project
        if ($request->isMethod('post')) {
            if ($request->getParameter('option') == "save") {
                if ($this->employeeResearchProjectPermissions->canCreate() || $this->employeeResearchProjectPermissions->canUpdate()) {

                    $this->researchProjectForm->bind($request->getParameter($this->researchProjectForm->getName()));

                    if ($this->researchProjectForm->isValid()) {
                        $researchProject = $this->getResearchProject($this->researchProjectForm);

                        $this->getEmployeeService()->saveEmployeeResearchProject($researchProject);
                        $this->getUser()->setFlash('researchProject.success', __(TopLevelMessages::SAVE_SUCCESS));
                    } else {
                        $this->getUser()->setFlash('researchProject.warning', __('Form Validation Failed.'));
                    }
                }
            }

            //this is to delete research project
            if ($request->getParameter('option') == "delete") {
                if ($this->employeeResearchProjectPermissions->canDelete()) {
                    $deleteIds = $request->getParameter('delResearchProject');

                    if (count($deleteIds) > 0) {
                        $this->getEmployeeService()->deleteEmployeeResearchProjectRecords($empNumber, $request->getParameter('delResearchProject'));
                        $this->getUser()->setFlash('researchProject.success', __(TopLevelMessages::DELETE_SUCCESS));
                    }
                }
            }
        }
        $this->getUser()->setFlash('qualificationSection', 'researchProject');
        $this->redirect('pim/viewQualifications?empNumber=' . $empNumber . '#researchProject');
    }

    private function getResearchProject(sfForm $form)
    {

        $post = $form->getValues();

        $researchProject = $this->getEmployeeService()->getEmployeeResearchProjectRecords($post['emp_number'], $post['seqno']);

        if (!$researchProject instanceof EmployeeResearchProject) {
            $researchProject = new EmployeeResearchProject();
        }

        $researchProject->emp_number = $post['emp_number'];
        $researchProject->seqno = $post['seqno'];
        $researchProject->name = $post['name'];
        $researchProject->role = $post['role'];
        $researchProject->start_date = $post['start_date'];
        $researchProject->end_date = $post['end_date'];
        $researchProject->status = $post['status'];

        return $researchProject;
    }

}

?>